package com.it.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.it.common.R;
import com.it.pojo.ApprovalRequest;
import com.it.pojo.Document;
import com.it.pojo.Log;
import com.it.service.DocumentService;
import com.it.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/document")
public class LogAndDocumentController {
    @Autowired
    private LogService logService;
    @Autowired
    private DocumentService documentService;
    @PostMapping("/logs")
    private R psotLogs(@RequestBody Log log){
        return logService.postLogs(log)?R.success("工作日志提交成功"):R.error("输入验证失败");
    }
    @GetMapping("/pending")
    public List<Log> getAllPendingLogs() {
        LambdaQueryWrapper<Log> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Log::getStatus, 0) // 0表示待审批
                .orderByDesc(Log::getCreateTime); // 按提交时间降序

        return logService.list(lqw);
    }
    @PostMapping("/approve")
    public boolean approveLog(@RequestBody ApprovalRequest request) {
            // 1. 保存新文档
            Document document = new Document();
            document.setContent(request.getContent());
            boolean saved = documentService.save(document);
        boolean b = logService.removeById(request.getLogId());
        return saved==b;
    }
    @PostMapping("/reject")
    public boolean deleteLog(@RequestBody ApprovalRequest request){
        return logService.removeById(request.getLogId());
    }
}
